#ifndef CONN_H
#define CONN_H


#define CONN_MAGIC_FDMSG         0x00010000   //fd传输
#define CONN_FD_REQUEST          0x00110000
#define CONN_FD_HANDLE           0x00210000
#define CONN_MAGIC_CMD           0x000f0000   //执行命令
#define CONN_CMD_STATUS          0x001f0000
#define CONN_CMD_START           0x002f0000
#define CONN_CMD_STOP            0x004f0000  
#define CONN_CMD_SHUTDOWN        0x008f0000   //关机命令


#define CONN_FDKIND_REG          0x00000001    //reg file fd
#define CONN_FDKIND_CHR          0x00000002    //char device
#define CONN_FDKIND_BLK          0x00000004    //block device
#define CONN_FDKIND_FIFO         0x00000008    //fifo fd 
#define CONN_FDKIND_SOCK         0x00000010    //socket fd 

#define CONN_MAGIC_INOTIFY       0x000a0000    //通知机制
#define CONN_INOTIFY_READY       0x001a0000    //就绪
#define CONN_INOTIFY_EXIT        0x002a0000    //退出


/* 
****General Message Agreement***
2.ascii码char值为32-127,proto需要小于32
 | (协议类型)4byte | (action长度)4byte | (data长度)4byte | <data> (max=1012)byte | 
*/
//rinit :通用信息协议header
typedef struct
{
    unsigned int     magic;      //协议标识
    unsigned int     action;     //协议动作
    unsigned int     payloadlen; //有效负载数据长度
}head_t;

//rinit :通用信息协议
typedef struct
{
    head_t header;
    char   data[1024];
} CONN_t;

//inotify
typedef struct
{
    void* ptr;
    int   num;
}Inotify;


//epoll events.data.ptr定义
typedef struct
{
    int            fd;
    unsigned int   flag;         //fd flag
    unsigned int   events;
    FILE*          stream;
    void*          extend;
    char           name[1024];
}epmsg_t; 


//process function in readlines 
typedef void (*io_work)(void *,char *,FILE *);

int pidfd_open(pid_t pid,unsigned int flags);
int pidfd_getfd(int pifd,int targetfd,unsigned int flags);
int fdcloexec(int fd);
int fdnonblock(int fd);
int fdtype(int fd);
epmsg_t *epmsg_init(int fd,int evts,FILE* stream,void *extend);
int epmsg_add(int epollfd,epmsg_t *epmsg);
int epmsg_del(int epollfd,epmsg_t *epmsg);

ssize_t sendfd(int sock_fd, int send_fd,const void *messages,unsigned int msglen);
ssize_t recvfd(int sock_fd,int *recv_fd,void *messages,unsigned int msglen);
void freadlines(void* user,FILE *__in,FILE *__out,unsigned int iosize,io_work __worker);
int listen_unix(const char *sock_path,int type,int listen_queue);
int connect_unix(const char *sock_path,int type);

int fdhandle(const char *sockpath,const char *msg);
int fdrequest(const char *_path,int *recv_fd,void *msg,unsigned int msglen);
void epollset(int epollfd,int action,epmsg_t *epmsg);
int inotify(const char *path,void *info,ssize_t n);

#endif